
Imports System.Net
Imports System.Net.Sockets
Imports Microsoft.Ink

Public Class InkConnection
    Private Owner As Form1 = Nothing

    Private Machine As IPEndPoint = Nothing
    Private Partner As IPEndPoint = Nothing

    Private InkLocal As TcpListener = Nothing
    Private InkRemote As TcpClient = Nothing

    Public Sub New(ByVal Owner As Form1)
        Me.Owner = Owner
    End Sub

    Public Sub Disconnect()
        InkLocal.Stop()
        InkRemote.Close()
        InkLocal = Nothing
        InkRemote = Nothing
    End Sub

    Public Sub Listen(ByVal Port As Integer)
        Me.Machine = New IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList(0), Port)
        InkLocal = New TcpListener(Me.Machine)

        InkLocal.Start()

        InkRemote = InkLocal.AcceptTcpClient()
    End Sub

    Public Sub Connect(ByVal Partner As String, ByVal Port As Integer)
        Me.Machine = New IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList(0), Port)
        Me.Partner = New IPEndPoint(Dns.Resolve(Partner).AddressList(0), Port)
        InkRemote = New TcpClient(Me.Machine)


        InkRemote.Connect(Me.Partner)
    End Sub

    Public Sub SendStroke(ByVal ThisStroke As Stroke)
        If InkRemote Is Nothing Then Exit Sub

        Dim i As Integer = 0

        Dim Stream As NetworkStream = InkRemote.GetStream()
        Dim Length As Short = ThisStroke.GetPoints().Length

        Stream.Write(BitConverter.GetBytes(Length), 0, 2)

        For i = 0 To Length - 1
            Dim ThisPoint As Point = ThisStroke.GetPoint(i)

            Stream.Write(BitConverter.GetBytes(ThisPoint.X), 0, 2)
            Stream.Write(BitConverter.GetBytes(ThisPoint.Y), 0, 2)
        Next
    End Sub



    Public Sub SendInk(ByVal A As ArrayList)
        If InkRemote Is Nothing Then Exit Sub

        Dim i As Integer = 0
        Dim Pa As New Pics
        Try
            Dim Stream As NetworkStream = InkRemote.GetStream
            Dim T(0) As Type
            T(0) = Pa.GetType
            Dim X As New Xml.Serialization.XmlSerializer(A.GetType, T)
            X.Serialize(Stream, A)
            '  Stream.Close()
        Catch ex As Exception

        End Try

    End Sub

    Public Function ReceiveInk() As ArrayList
        If InkRemote Is Nothing Then Return Nothing

        Dim i As Integer = 0

        Dim Stream As NetworkStream = InkRemote.GetStream()
        If Not Stream.DataAvailable Then Return Nothing

        Dim A As New ArrayList
        Dim Pa As New Pics
        Dim T(0) As Type
        T(0) = Pa.GetType
        Dim X As New Xml.Serialization.XmlSerializer(A.GetType, T)
        A = X.Deserialize(Stream)
        Stream.Close()

        Return A
    End Function

    Public Function ReceiveStroke() As Stroke
        If InkRemote Is Nothing Then Return Nothing

        Dim i As Integer = 0

        Dim Stream As NetworkStream = InkRemote.GetStream()
        If Not Stream.DataAvailable Then Return Nothing

        Dim Buffer(4) As Byte : Stream.Read(Buffer, 0, 2)
        Dim Length As Short = BitConverter.ToInt16(Buffer, 0)

        Dim ThesePoints(Length - 1) As Point

        For i = 0 To Length - 1
            Dim ThisPoint As New Point

            Stream.Read(Buffer, 0, 4)
            ThisPoint.X = BitConverter.ToInt16(Buffer, 0)
            ThisPoint.Y = BitConverter.ToInt16(Buffer, 2)

            ThesePoints(i) = ThisPoint
        Next

        Return Owner.InkBox.Ink.CreateStroke(ThesePoints)
    End Function
End Class
